/* eslint-disable max-classes-per-file */
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wiz
 * @Date: 2023-08-16 17:36:20
 * @LastEditors: zss
 * @LastEditTime: 2024-05-09 15:44:34
 */
import { N_oSetupPanel } from '../../types';
import { T_compPanelConf } from '../compConfPanel';
import { T_toolbarLib } from '../compToolbar';
import { T_compBase } from './compBase';
import { I_formPanel } from './types';

type I_tableStyle = N_oSetupPanel.N_comps.I_tableStyle;

type I_compToolbar = N_oSetupPanel.N_comps.I_compToolbar;
type I_messageConf = N_oSetupPanel.N_comps.I_messageConf;

export interface I_formStyle {
  columnNum: number;
  isBottom: boolean;
}

export class T_cBaseFormConf {
  name: string;

  comment: string;

  // mainFun: string;

  dbType: 'mysql' | 'mongo';

  isCreated: boolean;

  status: string;

  tableStyle: I_tableStyle;

  // 表格样式
  toolbar: I_compToolbar;

  messageConfig: I_messageConf;

  selbill: Array<any>;

  queryLink: Array<any>;

  panelLayout: object;

  formPanels: Array<I_formPanel>;

  action: object;
}
export class T_cBaseForm extends T_compBase {
  public conf: T_cBaseFormConf = new T_cBaseFormConf();

  private TtoolbarLib: T_toolbarLib = new T_toolbarLib();

  private TcompPanelConf: T_compPanelConf = new T_compPanelConf();

  constructor() {
    super();
    this.conf = {
      name: '',
      comment: '',
      // mainFun: '',
      isCreated: false,
      status: '',
      dbType: N_oSetupPanel.N_comps.DB_TYPE.MYSQL,
      tableStyle: {
        showHeader: true,
        showFooter: false,
        showPager: true,
        showToolbar: true,
        border: false,
        mode: N_oSetupPanel.N_comps.TABLESTYLE_MODE.MODE_NULL
      }, // 表格样式
      toolbar: null,
      messageConfig: {
        custDefListener: {
          search: [],
          coordination: null
        },
        emitActions: {
          search: [],
          refresh: []
        },
        eventGroup: null
      },
      selbill: [],
      queryLink: [],
      panelLayout: {
        formLayout: N_oSetupPanel.N_comps.LAYOUTSTYLE.TABS
      },
      formPanels: [
        {
          label: '',
          content: {
            name: '',
            formStyle: {
              // columnNum: 0,
              // pixelWidth: 0,
              // percentageWidth: 0,
              // isBottom: false
            },
            formColumns: [],
            uniqueIndexes: [],
            indexes: [],
            rules: [],
            dbType: ''
          }
        }
      ],

      action: {}
    };
    this.conf.toolbar = this.TtoolbarLib.getCompToolBarSet(
      N_oSetupPanel.N_comps.COMP_NAME.C_BASE_FORM
    );
  }

  // public addUniqueIndexes(...p: any[]): void {
  //   this.conf.uniqueIndexes.push(...p);
  // }

  // public addIndexes(...p: any[]): void {
  //   this.conf.indexes.push(...p);
  // }

  // public addRules(...p: any[]): void {
  //   this.conf.rules.push(...p);
  // }

  public getConf(): T_cBaseFormConf {
    return this.conf;
  }

  public getComponentId(): N_oSetupPanel.N_comps.COMP_MODULE_ID {
    return N_oSetupPanel.N_comps.COMP_MODULE_ID.BASE_FORM;
  }

  public GetCompPanelConf(): N_oSetupPanel.N_comps.I_setupPanel {
    return this.TcompPanelConf.getCompPanelSet(
      N_oSetupPanel.N_comps.COMP_NAME.C_BASE_FORM
    );
  }

  public getPermissionList(
    config: T_cBaseFormConf
  ): Array<N_oSetupPanel.I_permission> {
    return [
      {
        type: 'button',
        area: 'toolbar',
        data: this.getToolbarKey(config)
      },

      // {
      //   type: 'selbill',
      //   area: 'toolbar',
      //   data: this.getSelbillData()
      // },
      {
        type: 'customButton',
        area: 'toolbar',
        data: this.getPmsAddonData(config)
      },
      {
        type: 'columns',
        area: 'form',
        data: this.getPmsForm(config)
      }
    ];
  }

  private getToolbarKey(config: T_cBaseFormConf): Array<string> {
    return Object.keys(this.conf.toolbar);
  }

  // private getSelbillData(): Array<string> {
  //   return [];
  // }

  private getPmsForm(config: any): Array<string> {
    const arr: Array<string> = [];
    if (config?.formPanels) {
      config.formPanels.forEach(fp => {
        fp.content.formColumns.forEach(col => {
          arr.push(col.field);
        });
      });
    } else if (config?.formColumns) {
      config.formColumns.forEach(col => {
        arr.push(col.field);
      });
    }

    return arr;
  }

  private getPmsAddonData(config: T_cBaseFormConf): Array<string> {
    const arr: Array<string> = [];
    if (Array.isArray(config?.toolbar?.addon)) {
      config.toolbar.addon.forEach(ctm => {
        arr.push(ctm.title);
        if (ctm.children.length > 0) {
          // eslint-disable-next-line no-use-before-define
          handleAddonData(ctm.children);
        }
      });
    }

    function handleAddonData(children): void {
      for (const child of children) {
        arr.push(child.title);
        if (child.children.length > 0) {
          handleAddonData(child.children);
        }
      }
    }
    return arr;
  }
}
